@file:Suppress("unused")

package com.meelive.channel.helper

import android.content.Context
import android.util.Log
import com.meelive.channel.reader.ChannelReader
import com.meelive.channel.reader.IdValueReader
import java.io.File
import java.nio.ByteBuffer

/**
 * Created by leontli on 17/2/12.
 */
object ChannelReaderUtil {
    private const val TAG = "ChannelReaderUtil"
    private var mChannelCache: String? = null

    fun getChannel(context: Context): String? {
        if (mChannelCache == null) {
            var channel = getChannelByV2(context)
            if (channel == null) {
                channel = getChannelByV1(context)
            }
            mChannelCache = channel
        }
        return mChannelCache
    }

    /**
     * if apk use v2 signature , please use this method to get channel info
     *
     * @param context
     * @return
     */
    fun getChannelByV2(context: Context): String? {
        val apkPath = getApkPath(context) ?: return null
        val channel = ChannelReader.getChannelByV2(File(apkPath))
        Log.i(TAG, "getChannelByV2 , channel = $channel")
        return channel
    }

    /**
     * if apk only use v1 signature , please use this method to get channel info
     *
     * @param context
     * @return
     */
    fun getChannelByV1(context: Context): String? {
        val apkPath = getApkPath(context) ?: return null
        val channel = ChannelReader.getChannelByV1(File(apkPath))
        Log.i(TAG, "getChannelByV1 , channel = $channel")
        return channel
    }

    /**
     * get String value from apk by id in the v2 signature mode
     *
     * @param context
     * @param id
     * @return
     */
    fun getStringValueById(context: Context, id: Int): String? {
        val apkPath = getApkPath(context) ?: return null
        val value = IdValueReader.getStringValueById(File(apkPath), id)
        Log.i(TAG, "id = $id , value = $value")
        return value
    }

    /**
     * get byte[] from apk by id in the v2 signature mode
     *
     * @param context
     * @param id
     * @return
     */
    fun getByteValueById(context: Context, id: Int): ByteArray? {
        val apkPath = getApkPath(context) ?: return null
        return IdValueReader.getByteValueById(File(apkPath), id)
    }

    /**
     * find all Id-Value Pair from Apk in the v2 signature mode
     *
     * @param context
     * @return
     */
    fun getAllIdValueMap(context: Context): Map<Int, ByteBuffer>? {
        val apkPath = getApkPath(context) ?: return null
        return IdValueReader.getAllIdValueMap(File(apkPath))
    }

    /**
     * 获取已安装的APK路径
     *
     * @param context
     * @return
     */
    private fun getApkPath(context: Context): String? {
        var apkPath: String? = null
        try {
            val applicationInfo = context.applicationInfo
            apkPath = if (applicationInfo == null) {
                return null
            } else {
                applicationInfo.sourceDir
            }
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return apkPath
    }
}